import Vue from 'vue';
import { Component, Emit, Prop, Provide, Watch } from 'vue-property-decorator'

import * as KeyParser from 'src/utils/key_parser';

@Component({})
export default class JsonView extends Vue {
    private init(): void {
        Excel.run(async (context: Excel.RequestContext) => {
            const ws: Excel.Worksheet = context.workbook.worksheets.getActiveWorksheet();
            ws.getRange('a1:b2').format.fill.color = 'green';
            await context.sync();
        })
    }


    @Prop({
        type: Object,
    }) nodeList: JsonNode[];

    @Prop({
        type: Object,
    }) rawData: any;

    render(h: any): any {
        var list:JsonNode[] = [];
        this.createNodeList(this.rawData, list, '', 0);
        return h(list);
    }



    private createNodeList(data: any, parent: any[], nodeKey:string, level?: number): void {
        if (!level) {
            level = 0;
        }
        for (var key in data) {
            var tmp: JsonNode = {} as any;
            tmp.level = level;
            tmp.key = nodeKey + "." + key;
            tmp.key = KeyParser.removeFirstDot(tmp.key);
            if (wlIsArray(data[key])) {
                tmp.children = [];
                tmp.type = 'array';
                tmp.isOpen = false;
                let lv: number = level + 1;
                let k:string = tmp.key + '[]';
                data[key].forEach((item: any) => {
                    this.createNodeList(item, tmp.children, k, lv);
                });
            }else if(wlIsObject(data[key])){
                tmp.children = [];
                tmp.type = 'object';
                tmp.isOpen = false;
                let lv: number = level + 1;
                this.createNodeList(data[key], tmp.children, tmp.key, lv);
            }
            tmp.val = data[key];
            parent.push(tmp);

        }
    }


    private createNode(key: string, level: number): JsonNode {
        var tmp: JsonNode = {} as any;

        return tmp;
    }


}
